
package com.hlkj.pay.controller.openapi;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.IMerchantReportAppService;
import com.hlkj.pay.app.openapi.IOpenApiAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.dto.merchant.report.MerchantReportDto;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppChannelDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantReportInfoDO;
import com.hlkj.pay.service.merchant.IMerchantApplicationService;
import com.hlkj.pay.util.OpenApiParamsUtils;
import com.hlkj.pay.vo.merchant.req.report.MerchantReportReq;
import com.hlkj.pay.vo.openapi.merchant.req.report.OpenApiMerchantReportQueryReq;
import com.hlkj.pay.vo.openapi.merchant.req.report.OpenApiMerchantReportReq;
import com.hlkj.pay.vo.openapi.merchant.resp.report.OpenApiMerchantReportResp;
import com.hlkj.pay.vo.order.OpenApiResult;
import com.hlkj.pay.vo.order.req.OrderCommonParamReq;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/09 11:25
 */
@Tag(name = "汇电通-报备管理")
@RestController
@RequestMapping("/openapi/report")
@RequiredArgsConstructor
@Slf4j
public class OpenApiMerchantReportController {

    private final IOpenApiAppService openApiAppService;

    private final IMerchantApplicationService merchantApplicationService;

    private final IMerchantReportAppService merchantReportAppService;

    @Operation(summary = "商户信息录入")
    @PostMapping("/submit")
    OpenApiResult<OpenApiMerchantReportResp> submitAccess(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiMerchantReportReq merchantReportReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiMerchantReportReq.class);
            CommonResult<MerchantReportDto> stringOpenApiResult = merchantReportAppService.submitReport(merchantReportReq);
            if (stringOpenApiResult.isSuccess()) {
                OpenApiMerchantReportResp openApiMerchantAccessResp = OpenApiParamsUtils.openApiMerchantReportResp(stringOpenApiResult.getData());
                return openApiAppService.sign(CommonResult.success(openApiMerchantAccessResp));
            }
            return OpenApiResult.error(stringOpenApiResult.getCode(), stringOpenApiResult.getMsg());
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "申请单状态查询")
    @PostMapping("/detail")
    OpenApiResult<OpenApiMerchantReportResp> accessDetail(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiMerchantReportQueryReq accessQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiMerchantReportQueryReq.class);
            CommonResult<MerchantReportDto> stringOpenApiResult = merchantReportAppService.reportDetail(accessQueryReq);
            if (stringOpenApiResult.isSuccess()) {
                OpenApiMerchantReportResp openApiMerchantAccessResp = OpenApiParamsUtils.openApiMerchantReportResp(stringOpenApiResult.getData());
                MerchantReportInfoDO merchantAccessInfo = stringOpenApiResult.getData().getMerchantReportInfo();
                if (MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS.getCode().equals(merchantAccessInfo.getStatus())) {
                    MerchantAppChannelDO merchantAppChannelDO = merchantApplicationService.queryDetailByAccessId(merchantAccessInfo.getId(),
                            MerchantEnum.DATA_RELATION_TYPE.REPORT);
                    if (merchantAppChannelDO != null) {
                        openApiMerchantAccessResp.setMchChannelCode(merchantAppChannelDO.getCode());
                    }
                }
                return openApiAppService.sign(CommonResult.success(openApiMerchantAccessResp));
            }
            return OpenApiResult.error(stringOpenApiResult.getCode(), stringOpenApiResult.getMsg());
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

}
